from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from orders.services import get_user_coupon_list, get_user_enable_coupon_list
from rest_framework.generics import CreateAPIView, ListAPIView
from orders.serializers import OrderModelSerializer, OrderListModelSerializer, OrderDetailModelSerializer
from orders.models import OrdersModel, OrdersDetailModel
from rest_framework.pagination import PageNumberPagination
from rest_framework import status
from doctors.models import AnswerModel, DoctorModel
from django.db.models import Q
from doctors.serializers import AnswerModelSerializer, DoctorModelSerializer
from patients.serializers import PatientListModelSerializer
from patients.models import PatientModel


class CouponListView(APIView):
    permission_classes = [IsAuthenticated, ]

    def get(self, request):
        user_id = request.user.id
        coupon_list = get_user_coupon_list(user_id)
        return Response({'message': '优惠劵列表获取成功', 'coupon': coupon_list})


class EnableCouponListView(APIView):
    permission_classes = [IsAuthenticated, ]

    def post(self, request):
        user_id = request.user.id
        print(user_id)
        # user_id = 2
        doctor_id = request.data.get('doctor_id')
        print(doctor_id)
        data = get_user_enable_coupon_list(user_id, doctor_id)
        return Response({'message': '可用优惠劵查询成功', 'coupon_list': data})


# 生成订单
class OrderCreateApiView(CreateAPIView):
    permission_classes = [IsAuthenticated, ]
    queryset = OrdersModel.objects.all()
    serializer_class = OrderModelSerializer


# 获取订单状态选项
class OrderPayStatusChoicesApiView(APIView):

    def get(self, request):
        return Response(OrdersModel.ORDER_STATUS_CHOICES)


# 分页器
class OrderListPageNumberPagination(PageNumberPagination):
    page_size = 5
    page_query_param = 'page'
    page_size_query_param = 'size'
    max_page_size = 10


# 订单列表
class OrderListApiView(ListAPIView):
    permission_classes = [IsAuthenticated, ]
    pagination_class = OrderListPageNumberPagination
    # queryset =
    serializer_class = OrderListModelSerializer

    def get_queryset(self):
        user = self.request.user
        query_set = OrdersModel.objects.filter(user=user, is_deleted=False, is_show=True)

        order_status = int(self.request.query_params.get('status', -1))
        # 动态获取配置的订单状态列表
        status_list = [i[0] for i in OrdersModel.ORDER_STATUS_CHOICES]
        # 匹配前端传递的状态，是否存在状态列表中
        if order_status in status_list:
            query_set = query_set.filter(order_status=order_status)

        return query_set.order_by('-id')


class OrderAPIView(APIView):
    permission_classes = [IsAuthenticated, ]

    def get(self, request):
        user_id = request.user.id
        print(user_id)
        order_list = OrdersModel.objects.filter(Q(user_id=user_id) & Q(order_status=1)).order_by('-pay_time')
        print(order_list)
        if len(order_list) < 1:
            return Response({'message': "该用户没有已支付的问题，请提交问题支付后再查看"}, status=status.HTTP_204_NO_CONTENT)
        data = []
        for order in order_list:
            order_detail_info = OrdersDetailModel.objects.filter(order__exact=order.id)
            for order_detail in order_detail_info:
                answer_info = AnswerModel.objects.filter(id__exact=order_detail.answer.id)
                for answer in answer_info:
                    doctor_info = DoctorModel.objects.filter(id__exact=answer.doctor.id)
                    patients_info = PatientModel.objects.filter(id__exact=answer.patients.id)
                    order_ser = OrderModelSerializer(instance=order_list, many=True)
                    order_detail_ser = OrderDetailModelSerializer(instance=order_detail_info, many=True)
                    patients_info_ser = PatientListModelSerializer(instance=patients_info, many=True)
                    answer_ser = AnswerModelSerializer(instance=answer_info, many=True)
                    doctor_info_ser = DoctorModelSerializer(instance=doctor_info, many=True)
                    data.append({
                        "order_info": order_ser.data,
                        "patients_info": patients_info_ser.data,
                        "answer_info": answer_ser.data,
                        "doctor_info": doctor_info_ser.data,
                        'order_detail_ser': order_detail_ser.data
                    })

        return Response({"message": "订单列表查询成功", 'data': data}, status=status.HTTP_200_OK)
